package icu.ac.kr.phonebook.activity;

import icu.ac.kr.phonebook.R;
import icu.ac.kr.phonebook.provider.ConstantProvider;
import icu.ac.kr.phonebook.provider.PacelableObject;
import icu.ac.kr.phonebook.provider.PacelableObject2;
import icu.ac.kr.phonebook.provider.ReservedWork;
import icu.ac.kr.phonebook.view.ScheduleDetail;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

public class ViewSchedule extends Activity {

	static final String TAG = "ViewSchedule";
	static String year;
	static String month;
	static String date;
	static int mode;
	ScheduleDetail detail;
	HashMap<Integer, ReservedWork> scheduledMap;
	HashMap<Integer, ReservedWork> reservedMap;
	HashSet<Integer> deletedSet = new HashSet<Integer>();
	ScrollView scroll; 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewschedule);
		
		TextView title = (TextView) findViewById(R.id.viewScheduleTitleText);
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		bundle.setClassLoader(getClassLoader());    
		
		Parcelable[] parcel =bundle.getParcelableArray("schedule");
		setScheduleMap(parcel[0], parcel[1]);
 
 		year = bundle.getString("year");
		month = bundle.getString("month");
		date = bundle.getString("date");
		mode = bundle.getInt("mode");
		String from = bundle.getString("from");
		
		title.setText(date +" "+ConstantProvider.getMonth(Integer.parseInt(month))+" "+year);
		scroll = (ScrollView) findViewById(R.id.scrollDetail);
		detail = new ScheduleDetail(this, null);
		detail.setActionListener((from.equals("ViewDayDiary"))?0:1);
		
		Button saveBtn = (Button) findViewById(R.id.schedule_save_btn);
		saveBtn.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				// TODO Auto-generated method stub
			
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putParcelableArray("scheduled", new Parcelable[]{new PacelableObject(reservedMap), new PacelableObject(scheduledMap), new PacelableObject2(deletedSet) });
				
				intent.putExtras(bundle);
				setResult(RESULT_OK, intent);
				
				finish();
				
			}
			
		});
		detail.bindData(reservedMap, scheduledMap, new String[]{year, month, date, ""+mode});
		scroll.addView(detail);
				
	}
	
		
	private void setScheduleMap(Parcelable reserved, Parcelable scheduled) {
		// TODO Auto-generated method stub
		 
		reservedMap = ((PacelableObject)reserved).work;
 		scheduledMap = ((PacelableObject)scheduled).work;
 		
 		
	 	Set<Integer> set1 = reservedMap.keySet();
	 	Iterator<Integer> it3 = set1.iterator();
	 	while(it3.hasNext())
	 	{
	 		ReservedWork entity = reservedMap.get(it3.next());
	 		Log.d(TAG,entity.hour +" mode : "+ entity.mode);
	 	}
	 	
	 	
	 	Set<Integer> set2 = scheduledMap.keySet();
	 	Iterator<Integer> it2 = set2.iterator();
	 	while(it2.hasNext())
	 	{
	 		ReservedWork entity = scheduledMap.get(it2.next());
	 		Log.d(TAG,entity.hour +" mode : "+ entity.mode);
	 	}
	 	
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume(); 
		scroll.removeView(detail);
		detail = new ScheduleDetail(this, null);
		detail.bindData(reservedMap, scheduledMap, new String[]{year, month, date, ""+mode});
		scroll.addView(detail);
			
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		switch(requestCode)
		{
		case 0:
				if (resultCode == RESULT_OK)
				{
					Bundle bundle = data.getExtras();
					
					Integer iKey = Integer.parseInt(bundle.getString("hour"));
					
					if (reservedMap.containsKey(iKey))
					{
						reservedMap.remove(Integer.parseInt(bundle.getString("hour")));
						
						if (!bundle.getString("workType").equals("cancel"))
								reservedMap.put(Integer.parseInt(bundle.getString("hour")), new ReservedWork(Integer.parseInt(bundle.getString("hour")), 
											bundle.getString("workType"), bundle.getString("comment"), bundle.getInt("mode")));
					}
					else if(scheduledMap.containsKey(iKey))
					{
						
						scheduledMap.remove(Integer.parseInt(bundle.getString("hour")));
						deletedSet.add(Integer.parseInt(bundle.getString("hour")));
						
						if (!bundle.getString("workType").equals("cancel"))
							reservedMap.put(Integer.parseInt(bundle.getString("hour")), new ReservedWork(Integer.parseInt(bundle.getString("hour")),
								bundle.getString("workType"), bundle.getString("comment"), bundle.getInt("mode") ));
					
					}
	
					onResume();
					
				}
	 
		
		}
		
	}
	
	
}
